﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Kopytko
{
    /// <summary>
    /// Interaction logic for GamesList.xaml
    /// </summary>
    public partial class GamesList : UserControl
    {
        public event EventHandler<GameChosenEventArgs> GameChosen;

        public PluginManager Plugins { get; set; }


        public GamesList()
        {
            Plugins = PluginManager.Instance;
            Plugins.LoadComponentsProgressChanged += new EventHandler<PluginManager.LoadComponentsProgressChangedEventArgs>(Plugins_LoadComponentsProgressChanged);
            Plugins.LoadComponentsCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Plugins_LoadComponentsCompleted);
            

            InitializeComponent();

            
        }

        void Plugins_LoadComponentsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate()
            {
                BusyIndicatorBehavior.SetBusyState(grid_main, !BusyIndicatorBehavior.GetBusyState(grid_main));
                lb_main.DataContext = Plugins;
            });
            
        }

        void Plugins_LoadComponentsProgressChanged(object sender, PluginManager.LoadComponentsProgressChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }


        public void Load(string path)
        {
            BusyIndicatorBehavior.SetBusyState(grid_main, !BusyIndicatorBehavior.GetBusyState(grid_main));

            Plugins.LoadComponentsAsync(path);
        }

        private void lb_main_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lb_main.SelectedItem is GameBase.IGame)
            {
                OnGameChosen(new GameChosenEventArgs()
                                 {
                                     SelectedGame = lb_main.SelectedItem as GameBase.IGame,
                                 });
            }
        }


        void OnGameChosen(GameChosenEventArgs e)
        {
            if (GameChosen!=null)
            {
                GameChosen(this, e);
            }
        }
    }

    public class GameChosenEventArgs : EventArgs
    {
        public GameBase.IGame SelectedGame;
    }
}
